#include "colorbutton.h"
#include <QPainter>
#include <QColorDialog>

ColorButton::ColorButton(QWidget *parent) :
    QAbstractButton(parent)
{
    setFocusPolicy(Qt::TabFocus);
    _color = palette().color(QPalette::Button);
    _inside = false;
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(this, SIGNAL(clicked()) , this, SLOT(changeColor()));
}
//-----------------------------------------------------------------------------

void ColorButton::setColor(const QColor &color)
{
    if (color != _color) {
        _color = color;
        update();
    }
}
//-----------------------------------------------------------------------------

QColor ColorButton::color() const
{
    return _color;
}
//-----------------------------------------------------------------------------

QSize ColorButton::sizeHint() const
{
    return QSize(35, 35);
}
//-----------------------------------------------------------------------------

QSize ColorButton::minimumSizeHint() const
{
    return QSize(20, 20);
}
//-----------------------------------------------------------------------------

void ColorButton::paintEvent(QPaintEvent */*event*/)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    int side = qMin(width(), height());
    painter.setViewport((width() - side)/2, (height() - side)/2, side, side);
    painter.setWindow(-50, -50, 100, 100);
    draw(&painter);
}
//-----------------------------------------------------------------------------

void ColorButton::draw(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(palette().dark());
    painter->drawRoundRect(-50, -50, 100 - depth, 100 - depth, roundness, roundness);
    painter->setBrush(palette().light());
    painter->drawRoundRect(-50 + depth, -50 + depth, 100 - depth, 100 - depth,
                           roundness, roundness);
    painter->setBrush(Qt::black);
    painter->drawRoundRect(-50 + depth, -50 + depth, 100 - 2*depth, 100 - 2*depth,
                           roundness, roundness);
    if (_inside && !isDown())
        painter->setBrush(_color.lighter(115));
    else
        painter->setBrush(_color);

    QRectF buttonRect(-50 + depth, -50 + depth, 100 - 2*depth, 100 - 2*depth);

    if (isDown()) {
        buttonRect.adjust(3, 3, -3, -3);
        buttonRect.translate(0.0, 1.5);
    }
    else
        buttonRect.adjust(1, 1, -1, -1);

    painter->drawRoundRect(buttonRect, roundness, roundness);
    QPainterPath btnFramePath;
    btnFramePath.addRoundRect(buttonRect, roundness);
    painter->setClipPath(btnFramePath);
    if (_inside && !isDown())
        painter->setBrush(_color);
    else
        painter->setBrush(_color.darker(115));

    painter->drawEllipse(QPoint(60, 60), 100, 100);
    painter->restore();
}
//-----------------------------------------------------------------------------

void ColorButton::changeColor()
{
    QColor color = QColorDialog::getColor(_color, this);
    if (color.isValid() && color != _color) {
        _color = color;
        update();
        emit colorChanged(_color);
    }
}
//-----------------------------------------------------------------------------

void ColorButton::leaveEvent(QEvent */*event*/)
{
    _inside = false;
    update();
}
//-----------------------------------------------------------------------------

void ColorButton::enterEvent(QEvent */*event*/)
{
    _inside = true;
    update();
}
//-----------------------------------------------------------------------------
